var app = new Vue({
    el: '#app',
    data: {
        stu:{},
        department:[],
        grades:[],
        selectedDept: 0,
        selectedGrade: 0,
        currentDistrict: '',
        currentHealth: '',
        currentTemperature:null
    },
    methods: {
        submitForm() {
            if (!this.checkField())
                return;
            let formData = this.encapsulateForm();
            this.$http.post('report/postReportRecords', formData).then(function(res){
                if (res.body.success) {
                    console.log('登记成功！');
                    window.location.href = "frontReportSuccess";
                }  else {
                    alert('登记失败！' + res.body.errMsg);
                }
            },function(){
                console.log('请求失败处理');
            });
        },
        checkField() {
            if (this.currentHealth == '' || this.currentDistrict == '' || this.currentTemperature == null) {
                console.log("请检查上报信息");
                alert("请检查上报信息");
                return false;
            }
            if (this.currentTemperature < 35.0 || this.currentTemperature > 42.0) {
                alert("体温数值不合法");
                return false;
            }
            return true;
        },
        encapsulateForm() {
            let formData = new FormData();
            let _this = this;
            //下面是表单绑定的data 数据
            formData.append('stuId', this.stu.stuId);
            formData.append('name', this.stu.name);
            formData.append('department', this.stu.department);
            this.grades.forEach(function (item, index) {
                if (item.gradeId == _this.selectedGrade) {
                    console.log('选中-->'+item.gradeName);
                    formData.append('inClass', item.gradeName);
                }
            });
            formData.append('currentDistrict', this.currentDistrict);
            let health = 0;
            if (this.currentHealth == '感染') {
                health = -1;
            } else if (this.currentHealth == '疑似') {
                health = 1;
            }
            formData.append('currentHealth', health);
            formData.append('currentTemperature',this.currentTemperature);
            return formData;
        }
    },
    created() {
        this.$http.get('report/initReportRecords').then(function(res){
            if (res.body.success) {
                console.log(res.body);
                let result = res.body.data;
                this.stu = result.student;
                this.department.push(result.department);
                this.grades = result.grade;
                // 注意this实例在不同域内 this对象不仅仅是vue
                let _this = this;
                this.grades.forEach(function (item, index) {
                    if (item.gradeName == _this.stu.inClass) {
                        _this.selectedGrade = item.gradeId;
                    }
                });
            }
        },function(){
            console.log('请求失败处理');
        });
    }
});

